<?php
/**
 * Created by PhpStorm.
 * User: 魏爷
 * Date: 2016/4/19
 * Time: 21:37
 */

namespace Home\Controller;


use Think\Controller;
use Think\Verify;

class UserLoginController extends Controller
{
    /**
     * 跳转登录界面
    **/
    public function userLogin(){
        $this->display('index/userLogin');
    }
    /**
     * 获取用户ID，用户跳转
     **/
    public function userLoginSkip(){
        $u_id=session('u_id');
        if(empty($u_id)){
            $this->display('index/userLogin');
        }else{
            $userName=getUserName();
            $libraryInfo=D('Library')->libraryInfo();
            $this->assign('_libraryInfo',$libraryInfo);
            $this->assign('_userName',$userName);
            $this->display('Public/common');
        }
    }
    /**
     * 用户验证
     **/
    public function userVerify(){
        $u_email=I('u_email');
        $u_pwd=I('u_pwd');
        $code = I('verify');
        if(empty($u_email)||empty($u_pwd)){
            $this->error('信息不能为空!');
        }
        if(!$this->checkVerify($code)){
            $this->error('验证码错误!');
        }
        $email=D('UserAdd')->getUserEmail($u_email);
        if($email==false){
            $this->error('用户邮箱不存在，请重新输入！');
        }
        $getDel=D('UserLogin')->getDel($u_email);
        if($getDel==false){
            $this->error('您的账户已被禁用，请及时联系管理员');
        }
        $res=D('UserLogin')->userLoginSkip($u_pwd,$u_email);
        if($res==false){
            $this->error('密码错误，请重新登录！');
        }else{
            $this->success('登陆成功！',U('Home/UserLogin/userLoginSkip'));
        }
    }
    /* 验证码 */
    public function verify(){
        $verify = new \Think\Verify();
        $verify->fontSize = 18;
        $verify->length   = 4;
        $verify->useNoise = false;
        $verify->imageW=130;
        $verify->imageH=50;
        $verify->entry();
    }
    /* 检查验证码 */
    private function checkVerify($code){
        $verify = new Verify();
        return $verify->check($code);
    }
}